from django.db import models
from django.utils import timezone
from user_auth.models import User
from notes.models import Note

class Comment(models.Model):
    """评论模型，支持多级回复"""
    id = models.BigAutoField(primary_key=True)
    note = models.ForeignKey(
        Note, 
        on_delete=models.CASCADE, 
        related_name='comments',
        verbose_name='所属笔记'
    )
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        related_name='comments',
        verbose_name='评论用户'
    )
    content = models.TextField(max_length=1000, verbose_name='评论内容')
    parent_comment = models.ForeignKey(
        'self', 
        on_delete=models.CASCADE, 
        null=True, 
        blank=True, 
        related_name='replies',
        verbose_name='父评论'
    )
    created_at = models.DateTimeField(default=timezone.now, verbose_name='评论时间')
    
    class Meta:
        verbose_name = '评论'
        verbose_name_plural = '评论'
        ordering = ['created_at']
    
    def __str__(self):
        if self.parent_comment:
            return f"{self.user.username} 回复 {self.parent_comment.user.username}: {self.content[:30]}..."
        return f"{self.user.username} 评论了 {self.note.title}: {self.content[:30]}..."